package anton.lunch;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.sql.DataSource;

@ManagedBean(name = "lunchBean")
@SessionScoped
public class LunchBean implements Serializable {

    @Resource(name = "jdbc/antonsDatasource")
    private DataSource ds;
    List<Lunch> lunches;
    private Calendar cal;
    private SimpleDateFormat sdf;
    private String dateToday;
    private String tempDate;

    public List<Lunch> getLunches() {
        return lunches;
    }
    
    public void loadTodaysLunches(){
        cal = Calendar.getInstance();
        this.dateToday = sdf.format(cal.getTime());
        try {
            loadLunches(dateToday);
        } catch (SQLException ex) {
            Logger.getLogger(LunchBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void loadDayLunches(int days){
        cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.add(Calendar.DAY_OF_YEAR, days);
        tempDate = sdf.format(cal.getTime());
        try {
            loadLunches(tempDate);
        } catch (SQLException ex) {
            Logger.getLogger(LunchBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void loadLunches(String date) throws SQLException {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet result = null;
        lunches.clear();

        try {
            if (ds == null) {
                throw new SQLException("Can't get data source");
            }

            con = ds.getConnection();

            if (con == null) {
                throw new SQLException("Can't get database connection");
            }

            ps = con.prepareStatement("SELECT m.lunch_date, l.lunch_name, l.lunch_description, l.lunch_price FROM Lunch l, LunchMenu m WHERE l.lunch_name=m.lunch_name AND m.lunch_date=\"" + date + "\"");

            result = ps.executeQuery();

            while (result.next()) {
                Lunch temp = new Lunch();

                temp.setLunch_date(result.getString("lunch_date"));
                temp.setLunch_name(result.getString("lunch_name"));
                temp.setLunch_description(result.getString("lunch_description"));
                temp.setLunch_price(result.getInt("lunch_price"));

                lunches.add(temp);
            }
        } finally {
            closeAll(result, ps, con);
        }
    }

    private static void closeAll(ResultSet resultSet, PreparedStatement preState, Connection con) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
            }
        }
        if (preState != null) {
            try {
                preState.close();
            } catch (SQLException e) {
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
            }
        }
    }

    public LunchBean() {
        sdf = new SimpleDateFormat("yyyy-MM-dd");
        cal = Calendar.getInstance();
        this.dateToday = sdf.format(cal.getTime());
        lunches = new ArrayList<>();
    }

}
